package com.example.seabedmusic.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.seabedmusic.R;
import com.example.seabedmusic.entity.HotSong;
import com.example.seabedmusic.views.HotMusicPlayerActivity;
import com.example.seabedmusic.views.MusicPlayerActivity;

import java.util.ArrayList;
import java.util.List;

public class HotSongAdapter extends RecyclerView.Adapter<HotSongAdapter.ViewHolder> {

    private List<HotSong> hotSongList;

    public HotSongAdapter(List<HotSong> hotSongList) {
        this.hotSongList = hotSongList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_hot_song, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HotSong song = hotSongList.get(position);

        // 设置排名
        holder.rankTv.setText(String.valueOf(position + 1));

        // 设置歌曲信息
        holder.songNameTv.setText(song.getName());
        holder.artistTv.setText(song.getAuthor());

        // 加载封面
        if (song.getCover() != null && !song.getCover().isEmpty()) {
            Glide.with(holder.itemView.getContext())
                    .load(song.getCover())
                    .placeholder(R.drawable.ic_music_note)
                    .error(R.drawable.ic_music_note)
                    .into(holder.coverIv);
        } else {
            holder.coverIv.setImageResource(R.drawable.ic_music_note);
        }

        // 点击事件  播放
        holder.itemView.setOnClickListener(v -> {
            // 创建Intent跳转到热歌播放页面
            Intent intent = new Intent(holder.itemView.getContext(), HotMusicPlayerActivity.class);

            // 传递当前歌曲数据
            intent.putExtra("song_title", song.getName());
            intent.putExtra("song_artist", song.getAuthor());
            intent.putExtra("song_cover", song.getCover());
            intent.putExtra("song_album", song.getAlbum());
            intent.putExtra("song_hash", song.getHash());
            intent.putExtra("song_rank", song.getRank());

            // 传递当前位置和整个热歌列表
            intent.putExtra("current_position", position);
            intent.putParcelableArrayListExtra("hot_song_list", new ArrayList<>(hotSongList));

            // 启动热歌播放页面
            holder.itemView.getContext().startActivity(intent);
        });
    }

    /**
     * 根据歌曲hash生成播放URL
     * 酷狗音乐的播放URL格式通常为：https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash={hash}
     */
    private String generatePlayUrl(String hash) {
        if (hash == null || hash.isEmpty()) {
            return "";
        }
        return "https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash=" + hash + "&mid=123456";
    }

    @Override
    public int getItemCount() {
        return hotSongList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView rankTv;
        ImageView coverIv;
        TextView songNameTv;
        TextView artistTv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            rankTv = itemView.findViewById(R.id.rank_tv);
            coverIv = itemView.findViewById(R.id.cover_iv);
            songNameTv = itemView.findViewById(R.id.song_name_tv);
            artistTv = itemView.findViewById(R.id.artist_tv);
        }
    }
}